1
|
|
|
import {writeFileSync} from 'fs' |
2
|
|
|
import Nginx from '../services/nginx' |
3
|
|
|
import nginxLaravelTemplate from '../templates/nginx/apps/laravel' |
4
|
|
|
import nginxMagento1Template from '../templates/nginx/apps/magento1' |
5
|
|
|
import nginxMagento2Template from '../templates/nginx/apps/magento2' |
6
|
|
|
import {ensureDirectoryExists} from '../utils/filesystem' |
7
|
|
|
import {getConfig, jaleSitesPath} from '../utils/jale' |
8
|
|
|
|
9
|
|
|
class SitesController { |
10
|
|
|
|
11
|
|
|
appTypes = ['laravel', 'magento2', 'magento1'] |
12
|
|
|
|
13
|
|
|
executeLink = async (type: string | undefined): Promise<void> => { |
14
|
|
|
const config = await getConfig() |
15
|
|
|
let appType = config.defaultTemplate |
16
|
|
|
|
17
|
|
|
if (type) |
18
|
|
|
appType = type |
19
|
|
|
|
20
|
|
|
if (!this.appTypes.includes(appType)) { |
21
|
|
|
console.log(`Invalid app type ${appType}. Please select one of: ${this.appTypes.join(', ')}`) |
22
|
|
|
return |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
const domain = process.cwd().substring(process.cwd().lastIndexOf('/') + 1) |
26
|
|
|
const hostname = `${domain}.${config.domain}` |
27
|
|
|
|
28
|
|
|
await ensureDirectoryExists(jaleSitesPath) |
29
|
|
|
|
30
|
|
|
this.createNginxConfig(appType, hostname) |
31
|
|
|
|
32
|
|
|
await (new Nginx()).reload() |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a Nginx template for the provided hostname with a specific template. |
37
|
|
|
* |
38
|
|
|
* @param appType |
39
|
|
|
* @param hostname |
40
|
|
|
*/ |
41
|
|
|
createNginxConfig = (appType: string, hostname: string): void => { |
42
|
|
|
switch (appType) { |
43
|
|
|
case 'magento2': |
44
|
|
|
writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxMagento2Template(hostname, process.cwd())) |
45
|
|
|
break |
46
|
|
|
case 'magento1': |
47
|
|
|
writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxMagento1Template(hostname, process.cwd())) |
48
|
|
|
break |
49
|
|
|
default: |
50
|
|
|
writeFileSync(`${jaleSitesPath}/${hostname}.conf`, nginxLaravelTemplate(hostname, process.cwd())) |
51
|
|
|
break |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
export default SitesController |